local DIY = require "packages/diy_utility/diy_utility"

local skel = fk.CreateSkill{
  name = "ym_water__yindou",
  tags = { Skill.Spirited },
  dynamic_desc = function (self, player, lang)
    if player:usedSkillTimes("ym_water__yindou",Player.HistoryGame) == 0 then
      return "昂扬技，若中央区牌数不少于轮次数，你可以将任意张牌当【决斗】使用，"..
      "然后你获得中央区的任意张牌，对方摸等量牌（手牌均不可超过五张）。<br>"..
      "<font color='grey'>激昂：当你成为伤害牌目标后，可以与一名其他角色拼点，若你没赢则此牌你无法响应。</font>"
    else
      return "<font color='grey'>昂扬技，若中央区牌数不少于轮次数，你可以将任意张牌当【决斗】使用，"..
      "然后你获得中央区的任意张牌，对方摸等量牌（手牌均不可超过五张）。</font><br>"..
      "激昂：当你成为伤害牌目标后，可以与一名其他角色拼点，若你没赢则此牌你无法响应。"
    end
  end,
}

Fk:loadTranslationTable{
  ["ym_water__yindou"] = "饮斗",
  [":ym_water__yindou"] = "昂扬技，若中央区牌数不少于轮次数，你可以将任意张牌当【决斗】使用，"..
  "然后你获得中央区的任意张牌，对方摸等量牌（手牌均不可超过五张）。<br>"..
  "激昂：当你成为伤害牌目标后，可以与一名其他角色拼点，若你没赢则此牌你无法响应。",

  ["#ym_water__yindou_choose"] = "饮斗：你可以将任意张牌当【决斗】使用，然后你获得中央区的任意张牌，对方摸等量牌（手牌均不可超过五张）",
  ["#ym_water__yindou_choose2"] = "饮斗：获得中央区的任意张牌，目标摸等量牌（手牌均不可超过五张）",
  ["#ym_water__yindou_choose3"] = "饮斗：激昂：你可以与一名其他角色拼点，若你没赢则此牌你无法响应",

  ["$ym_water__yindou1"] = "君壮情烈胆，某必当奉陪！",
  ["$ym_water__yindou2"] = "哼！汝还能战否？",
  ["$ym_water__yindou3"] = "待吾重振兵马，胜负犹未可知！",
  ["$ym_water__yindou4"] = "天道择义而襄，英雄待机而胜！",

}

skel:addAcquireEffect(function (self, player, is_start)--DIYU的中央区加入游戏
  player.room:addSkill("#CenterArea")
end)

skel:addEffect("viewas",{
  prompt = "#ym_water__yindou_choose",
  pattern = "duel",
  min_card_num = 1,
  card_filter = Util.TrueFunc,
  audio_index = {1,2},
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local card = Fk:cloneCard("duel")
    card:addSubcards(cards)
    card.skillName = skel.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    --动画
    room:doSuperLightBox("packages/water_wegame/qml/shitaishici.qml",{
      animIndex = 2
    })
    room:delay(1500)
    player.general = "ym_water__taishici2"
    room:broadcastProperty(player,"general")
  end,
  after_use = function (self, player, use)
    local room = player.room
    local allcards = DIY.getCenterCards(room)
    local maxcho = 5 - #player:getCardIds("h")
    if maxcho <= 0 then return end
    local cards = room:askToChooseCards(player,{
      target = player,
      skill_name = skel.name,
      flag = {card_data = {{"$CenterArea",allcards}}},
      min = 0,
      max = maxcho,
      prompt = "#ym_water__yindou_choose2",
    })
    if #cards > 0 then
      room:obtainCard(player,cards,true,fk.ReasonJustMove,player,skel.name)
      for _, to in ipairs(use.tos) do
        maxcho = 5 - #to:getCardIds("h")
        if maxcho <= 0 then return end
        room:drawCards(to,math.min(#cards,maxcho))
      end
    end
  end,
  enabled_at_play = function (self, player)
    return not player:isNude() and player:usedSkillTimes(skel.name,Player.HistoryGame) == 0 and #DIY.getCenterCards(Fk:currentRoom()) >= Fk:currentRoom():getBanner("RoundCount")
  end,
  enabled_at_response = function (self, player, response)
    return not response and not player:isNude() and player:usedSkillTimes(skel.name,Player.HistoryGame) == 0 and #DIY.getCenterCards(Fk:currentRoom()) >= Fk:currentRoom():getBanner("RoundCount")
  end,
})

skel:addEffect(fk.TargetConfirmed, {--激昂
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and data.card.is_damage_card == true and player:usedSkillTimes(skel.name, Player.HistoryGame) > 0 then
    -- 玩家有技能                      玩家成为目标          伤害牌                               发动过限定技
      return table.find(player.room:getOtherPlayers(player), function (p)
        return player:canPindian(p)
        --存在玩家可以拼点
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    player:setSkillUseHistory(skel.name, 0, Player.HistoryGame)
    local targets = table.filter(player.room:getOtherPlayers(player), function (p)
        return player:canPindian(p)
        --哪些可以被拼点
      end)
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = skel.name,
      prompt = "#ym_water__yindou_choose3",
      cancelable = true,
    })
    if not (#tos > 0) then return end
    --动画
    player:broadcastSkillInvoke(skel.name,table.random({3,4}))
    room:doSuperLightBox("packages/water_wegame/qml/shitaishici.qml",{
      animIndex = 3
    })
    room:delay(1500)
    player.general = "ym_water__taishici3"
    room:broadcastProperty(player,"general")
    --拼点
    local pindian = player:pindian(tos,skel.name)
    local rlt = pindian.results[tos[1]].winner
    --结果
    if rlt and rlt == player then
      return
    end
    --否则不能响应
    data.use.unoffsetableList = data.use.unoffsetableList or {}
    table.insertIfNeed(data.use.unoffsetableList, player)
  end,
})

return skel